﻿using System.Collections.Generic;
using Engine;
using Game;
using GameEntitySystem;
using static Game.SubsystemCreatureSpawn;

public class HDTestLoader2 : ModLoader
{
    public Game.Random m_random = new Game.Random();
    public SubsystemCreatureSpawn m_subsystemCreatureSpawn;
    public SubsystemGameInfo m_subsystemGameInfo;
    public SubsystemSpawn m_subsystemSpawn;
    public SubsystemTerrain m_subsystemTerrain;
    public SubsystemTime m_subsystemTime;
    public SubsystemSky m_subsystemSky;
    public SubsystemBodies m_subsystemBodies;
    public SubsystemGameWidgets m_subsystemViews;
    public List<CreatureType> m_creatureTypes;
    public SubsystemParticles m_subsystemParticles;

    public override void __ModInitialize()
    {
        ModsManager.RegisterHook("InitializeCreatureTypes", this);
        ModsManager.RegisterHook("OnMinerHit", this);
        ModsManager.RegisterHook("OnLevelUpdate", this);
    }



    public override void OnMinerHit(ComponentMiner miner, ComponentBody componentBody, Vector3 hitPoint, Vector3 hitDirection, ref float attackPower, ref float playerProbability, ref float creatureProbability, out bool Hitted)
    {
        Hitted = false;

        if (miner.ComponentPlayer == null)
            return;

        NK9 nk = miner.ComponentPlayer.Entity.FindComponent<NK9>();
        if (nk == null || nk.ID != 588)
            return;

        miner.ComponentPlayer.ComponentHealth.Health += 0.05f;

        if (miner.CanUseTool(miner.ActiveBlockValue) == null)
            return;

        Block block = BlocksManager.Blocks[Terrain.ExtractContents(miner.ActiveBlockValue)];
        float num123 = block.GetMeleePower(miner.ActiveBlockValue);

        for (int i = 20; i <= 100; i += 10)
        {
            if (num123 > i && miner.ComponentPlayer.PlayerData.Level < i + 10)
            {
                miner.ComponentPlayer.ComponentHealth.Injure(0.01f * num123, null, false, $"等级不足使用高级武器");
                miner.ComponentPlayer.ComponentGui.DisplaySmallMessage($"等级不足{i + 10}级，但伤害超过{i}，被武器反噬", Color.LightRed, blinking: true, playNotificationSound: false);
                Hitted = true;
                break;
            }
        }

        nk.XFBarWidget.Flash(10);
        nk.XF -= 0.02f;
    }





    public List<Entity> SpawnCreatures(CreatureType creatureType, string templateName, Point3 point, int count)
    {
        return m_subsystemCreatureSpawn.SpawnCreatures(creatureType, templateName, point, count);
    }
    public override void InitializeCreatureTypes(SubsystemCreatureSpawn spawn, List<SubsystemCreatureSpawn.CreatureType> creatureTypes)
    {
        m_creatureTypes = creatureTypes;
        m_subsystemCreatureSpawn = spawn;
        m_subsystemGameInfo = spawn.Project.FindSubsystem<SubsystemGameInfo>(throwOnError: true);
        m_subsystemSpawn = spawn.Project.FindSubsystem<SubsystemSpawn>(throwOnError: true);
        m_subsystemTerrain = spawn.Project.FindSubsystem<SubsystemTerrain>(throwOnError: true);
        m_subsystemTime = spawn.Project.FindSubsystem<SubsystemTime>(throwOnError: true);
        m_subsystemSky = spawn.Project.FindSubsystem<SubsystemSky>(throwOnError: true);
        m_subsystemBodies = spawn.Project.FindSubsystem<SubsystemBodies>(throwOnError: true);
        m_subsystemViews = spawn.Project.FindSubsystem<SubsystemGameWidgets>(throwOnError: true);
        base.InitializeCreatureTypes(spawn, m_creatureTypes);
        //前面不用动，下

        m_creatureTypes.Add(new CreatureType("混沌鲸", SpawnLocationType.Surface, randomSpawn: false, constantSpawn: false)
        {
            SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
            {
                float num91 = m_subsystemTerrain.TerrainContentsGenerator.CalculateOceanShoreDistance(point.X, point.Z); //海岸线距离
                int humidity24 = m_subsystemTerrain.Terrain.GetHumidity(point.X, point.Z); //湿度
                int num92 = Terrain.ExtractContents(m_subsystemTerrain.Terrain.GetCellValueFast(point.X, point.Y - 1, point.Z)); //脚下方块
                                                                                                                                 //return (num91 > 20f && humidity24 > 8 && point.Y < 80 && (num92 == 8 || num92 == 7 || num92 == 3 || num92 == 2)) ? 0.25f : 0f; 原生成条件
                return (num92 == 577) ? 0.05f : 0f; //新生成条件：脚下方块为铁块(id为46)时才会生成，生成权重由0.25改为2.5，权重越高，刷新概率越大，为0则不生成，7是沙子，8是土
            },
            SpawnFunction = ((CreatureType creatureType, Point3 point) => SpawnCreatures(creatureType, "混沌鲸", point, 1).Count)
        });
        m_creatureTypes.Add(new CreatureType("火鸡王", SpawnLocationType.Surface, randomSpawn: false, constantSpawn: false)
        {
            SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
            {
                float num91 = m_subsystemTerrain.TerrainContentsGenerator.CalculateOceanShoreDistance(point.X, point.Z); //海岸线距离
                int humidity24 = m_subsystemTerrain.Terrain.GetHumidity(point.X, point.Z); //湿度
                int num92 = Terrain.ExtractContents(m_subsystemTerrain.Terrain.GetCellValueFast(point.X, point.Y - 1, point.Z)); //脚下方块
                                                                                                                                 //return (num91 > 20f && humidity24 > 8 && point.Y < 80 && (num92 == 8 || num92 == 7 || num92 == 3 || num92 == 2)) ? 0.25f : 0f; 原生成条件
                return (num92 == 578) ? 0.05f : 0f; //新生成条件：脚下方块为铁块(id为46)时才会生成，生成权重由0.25改为2.5，权重越高，刷新概率越大，为0则不生成，7是沙子，8是土
            },
            SpawnFunction = ((CreatureType creatureType, Point3 point) => SpawnCreatures(creatureType, "火鸡王", point, 1).Count)
        });
        m_creatureTypes.Add(new CreatureType("白狼人", SpawnLocationType.Surface, randomSpawn: false, constantSpawn: false)
        {
            SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
            {
                float num91 = m_subsystemTerrain.TerrainContentsGenerator.CalculateOceanShoreDistance(point.X, point.Z); //海岸线距离
                int humidity24 = m_subsystemTerrain.Terrain.GetHumidity(point.X, point.Z); //湿度
                int num92 = Terrain.ExtractContents(m_subsystemTerrain.Terrain.GetCellValueFast(point.X, point.Y - 1, point.Z)); //脚下方块
                                                                                                                                 //return (num91 > 20f && humidity24 > 8 && point.Y < 80 && (num92 == 8 || num92 == 7 || num92 == 3 || num92 == 2)) ? 0.25f : 0f; 原生成条件
                return (num92 == 7) ? 0.05f : 0f; //新生成条件：脚下方块为铁块(id为46)时才会生成，生成权重由0.25改为2.5，权重越高，刷新概率越大，为0则不生成，7是沙子，8是土
            },
            SpawnFunction = ((CreatureType creatureType, Point3 point) => SpawnCreatures(creatureType, "白狼人", point, 1).Count)
        });
        m_creatureTypes.Add(new CreatureType("狼人[二阶]", SpawnLocationType.Surface, randomSpawn: false, constantSpawn: false)
        {
            SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
            {
                float num91 = m_subsystemTerrain.TerrainContentsGenerator.CalculateOceanShoreDistance(point.X, point.Z); //海岸线距离
                int humidity24 = m_subsystemTerrain.Terrain.GetHumidity(point.X, point.Z); //湿度
                int num92 = Terrain.ExtractContents(m_subsystemTerrain.Terrain.GetCellValueFast(point.X, point.Y - 1, point.Z)); //脚下方块
                                                                                                                                 //return (num91 > 20f && humidity24 > 8 && point.Y < 80 && (num92 == 8 || num92 == 7 || num92 == 3 || num92 == 2)) ? 0.25f : 0f; 原生成条件
                return (num92 == 7) ? 0.5f : 0f; //新生成条件：脚下方块为铁块(id为46)时才会生成，生成权重由0.25改为2.5，权重越高，刷新概率越大，为0则不生成，7是沙子，8是土
            },
            SpawnFunction = ((CreatureType creatureType, Point3 point) => SpawnCreatures(creatureType, "狼人[二阶]", point, 1).Count)
        });
        m_creatureTypes.Add(new CreatureType("黑化血化狼王", SpawnLocationType.Surface, randomSpawn: false, constantSpawn: false)
        {
            SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
            {
                float num91 = m_subsystemTerrain.TerrainContentsGenerator.CalculateOceanShoreDistance(point.X, point.Z); //海岸线距离
                int humidity24 = m_subsystemTerrain.Terrain.GetHumidity(point.X, point.Z); //湿度
                int num92 = Terrain.ExtractContents(m_subsystemTerrain.Terrain.GetCellValueFast(point.X, point.Y - 1, point.Z)); //脚下方块
                                                                                                                                 //return (num91 > 20f && humidity24 > 8 && point.Y < 80 && (num92 == 8 || num92 == 7 || num92 == 3 || num92 == 2)) ? 0.25f : 0f; 原生成条件
                return (num92 == 2 || num92 == 577) ? 0.05f : 0f; //新生成条件：脚下方块为铁块(id为46)时才会生成，生成权重由0.25改为2.5，权重越高，刷新概率越大，为0则不生成，7是沙子，8是土
            },
            SpawnFunction = ((CreatureType creatureType, Point3 point) => SpawnCreatures(creatureType, "黑化血化狼王", point, 1).Count)
        });
        m_creatureTypes.Add(new CreatureType("寒冰星君", SpawnLocationType.Surface, randomSpawn: false, constantSpawn: false)
        {
            SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
            {
                float num91 = m_subsystemTerrain.TerrainContentsGenerator.CalculateOceanShoreDistance(point.X, point.Z); //海岸线距离
                int humidity24 = m_subsystemTerrain.Terrain.GetHumidity(point.X, point.Z); //湿度
                int num92 = Terrain.ExtractContents(m_subsystemTerrain.Terrain.GetCellValueFast(point.X, point.Y - 1, point.Z)); //脚下方块
                                                                                                                                 //return (num91 > 20f && humidity24 > 8 && point.Y < 80 && (num92 == 8 || num92 == 7 || num92 == 3 || num92 == 2)) ? 0.25f : 0f; 原生成条件
                return (num92 == 577) ? 0.05f : 0f; //新生成条件：脚下方块为铁块(id为46)时才会生成，生成权重由0.25改为2.5，权重越高，刷新概率越大，为0则不生成，7是沙子，8是土
            },
            SpawnFunction = ((CreatureType creatureType, Point3 point) => SpawnCreatures(creatureType, "寒冰星君", point, 1).Count)
        });
        m_creatureTypes.Add(new CreatureType("异犀", SpawnLocationType.Surface, randomSpawn: false, constantSpawn: false)
        {
            SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
            {
                float num91 = m_subsystemTerrain.TerrainContentsGenerator.CalculateOceanShoreDistance(point.X, point.Z); //海岸线距离
                int humidity24 = m_subsystemTerrain.Terrain.GetHumidity(point.X, point.Z); //湿度
                int num92 = Terrain.ExtractContents(m_subsystemTerrain.Terrain.GetCellValueFast(point.X, point.Y - 1, point.Z)); //脚下方块
                                                                                                                                 //return (num91 > 20f && humidity24 > 8 && point.Y < 80 && (num92 == 8 || num92 == 7 || num92 == 3 || num92 == 2)) ? 0.25f : 0f; 原生成条件
                return (num92 == 577) ? 0.05f : 0f; //新生成条件：脚下方块为铁块(id为46)时才会生成，生成权重由0.25改为2.5，权重越高，刷新概率越大，为0则不生成，7是沙子，8是土
            },
            SpawnFunction = ((CreatureType creatureType, Point3 point) => SpawnCreatures(creatureType, "异犀", point, 1).Count)
        });
        m_creatureTypes.Add(new CreatureType("异牛", SpawnLocationType.Surface, randomSpawn: false, constantSpawn: false)
        {
            SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
            {
                float num91 = m_subsystemTerrain.TerrainContentsGenerator.CalculateOceanShoreDistance(point.X, point.Z); //海岸线距离
                int humidity24 = m_subsystemTerrain.Terrain.GetHumidity(point.X, point.Z); //湿度
                int num92 = Terrain.ExtractContents(m_subsystemTerrain.Terrain.GetCellValueFast(point.X, point.Y - 1, point.Z)); //脚下方块
                                                                                                                                 //return (num91 > 20f && humidity24 > 8 && point.Y < 80 && (num92 == 8 || num92 == 7 || num92 == 3 || num92 == 2)) ? 0.25f : 0f; 原生成条件
                return (num92 == 7 || num92 == 577) ? 0.05f : 0f; //新生成条件：脚下方块为铁块(id为46)时才会生成，生成权重由0.25改为2.5，权重越高，刷新概率越大，为0则不生成，7是沙子，8是土
            },
            SpawnFunction = ((CreatureType creatureType, Point3 point) => SpawnCreatures(creatureType, "异牛", point, 1).Count)
        });
        m_creatureTypes.Add(new CreatureType("血鲸", SpawnLocationType.Surface, randomSpawn: false, constantSpawn: false)
        {
            SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
            {
                float num91 = m_subsystemTerrain.TerrainContentsGenerator.CalculateOceanShoreDistance(point.X, point.Z); //海岸线距离
                int humidity24 = m_subsystemTerrain.Terrain.GetHumidity(point.X, point.Z); //湿度
                int num92 = Terrain.ExtractContents(m_subsystemTerrain.Terrain.GetCellValueFast(point.X, point.Y - 1, point.Z)); //脚下方块
                                                                                                                                 //return (num91 > 20f && humidity24 > 8 && point.Y < 80 && (num92 == 8 || num92 == 7 || num92 == 3 || num92 == 2)) ? 0.25f : 0f; 原生成条件
                return (num92 == 577) ? 0.05f : 0f; //新生成条件：脚下方块为铁块(id为46)时才会生成，生成权重由0.25改为2.5，权重越高，刷新概率越大，为0则不生成，7是沙子，8是土
            },
            SpawnFunction = ((CreatureType creatureType, Point3 point) => SpawnCreatures(creatureType, "血鲸", point, 1).Count)
        });


        m_creatureTypes.Add(new CreatureType("Boy2", SpawnLocationType.Surface, randomSpawn: false, constantSpawn: false)
        {
            SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
            {
                float num91 = m_subsystemTerrain.TerrainContentsGenerator.CalculateOceanShoreDistance(point.X, point.Z); //海岸线距离
                int humidity24 = m_subsystemTerrain.Terrain.GetHumidity(point.X, point.Z); //湿度
                int num92 = Terrain.ExtractContents(m_subsystemTerrain.Terrain.GetCellValueFast(point.X, point.Y - 1, point.Z)); //脚下方块
                                                                                                                                 //return (num91 > 20f && humidity24 > 8 && point.Y < 80 && (num92 == 8 || num92 == 7 || num92 == 3 || num92 == 2)) ? 0.25f : 0f; 原生成条件
                return (num92 == 8 || num92 == 2 || num92 == 7 || num92 == 3) ? 0.5f : 0f; //新生成条件：脚下方块为铁块(id为46)时才会生成，生成权重由0.25改为2.5，权重越高，刷新概率越大，为0则不生成，7是沙子，8是土
            },
            SpawnFunction = ((CreatureType creatureType, Point3 point) => SpawnCreatures(creatureType, "Boy2", point, 1).Count)
        });

        m_creatureTypes.Add(new CreatureType("MS", SpawnLocationType.Surface, randomSpawn: false, constantSpawn: false)
        {
            SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
            {
                float num91 = m_subsystemTerrain.TerrainContentsGenerator.CalculateOceanShoreDistance(point.X, point.Z); //海岸线距离
                int humidity24 = m_subsystemTerrain.Terrain.GetHumidity(point.X, point.Z); //湿度
                int num92 = Terrain.ExtractContents(m_subsystemTerrain.Terrain.GetCellValueFast(point.X, point.Y - 1, point.Z)); //脚下方块
                                                                                                                                 //return (num91 > 20f && humidity24 > 8 && point.Y < 80 && (num92 == 8 || num92 == 7 || num92 == 3 || num92 == 2)) ? 0.25f : 0f; 原生成条件
                return (num92 == 577 || num92 == 2 || num92 == 7 || num92 == 3) ? 0.09f : 0f; //新生成条件：脚下方块为铁块(id为46)时才会生成，生成权重由0.25改为2.5，权重越高，刷新概率越大，为0则不生成，7是沙子，8是土
            },
            SpawnFunction = ((CreatureType creatureType, Point3 point) => SpawnCreatures(creatureType, "MS", point, 1).Count)
        });

        m_creatureTypes.Add(new CreatureType("HT", SpawnLocationType.Surface, randomSpawn: false, constantSpawn: false)
        {
            SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
            {
                float num91 = m_subsystemTerrain.TerrainContentsGenerator.CalculateOceanShoreDistance(point.X, point.Z); //海岸线距离
                int humidity24 = m_subsystemTerrain.Terrain.GetHumidity(point.X, point.Z); //湿度
                int num92 = Terrain.ExtractContents(m_subsystemTerrain.Terrain.GetCellValueFast(point.X, point.Y - 1, point.Z)); //脚下方块
                                                                                                                                 //return (num91 > 20f && humidity24 > 8 && point.Y < 80 && (num92 == 8 || num92 == 7 || num92 == 3 || num92 == 2)) ? 0.25f : 0f; 原生成条件
                return (num92 == 577 || num92 == 2 || num92 == 7 || num92 == 3) ? 0.09f : 0f; //新生成条件：脚下方块为铁块(id为46)时才会生成，生成权重由0.25改为2.5，权重越高，刷新概率越大，为0则不生成，7是沙子，8是土
            },
            SpawnFunction = ((CreatureType creatureType, Point3 point) => SpawnCreatures(creatureType, "HT", point, 1).Count)
        });

        m_creatureTypes.Add(new CreatureType("炸麦", SpawnLocationType.Surface, randomSpawn: false, constantSpawn: false)
        {
            SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
            {
                float num91 = m_subsystemTerrain.TerrainContentsGenerator.CalculateOceanShoreDistance(point.X, point.Z); //海岸线距离
                int humidity24 = m_subsystemTerrain.Terrain.GetHumidity(point.X, point.Z); //湿度
                int num92 = Terrain.ExtractContents(m_subsystemTerrain.Terrain.GetCellValueFast(point.X, point.Y - 1, point.Z)); //脚下方块
                                                                                                                                 //return (num91 > 20f && humidity24 > 8 && point.Y < 80 && (num92 == 8 || num92 == 7 || num92 == 3 || num92 == 2)) ? 0.25f : 0f; 原生成条件
                return (num92 == 577 || num92 == 2 || num92 == 7 || num92 == 3) ? 0.09f : 0f; //新生成条件：脚下方块为铁块(id为46)时才会生成，生成权重由0.25改为2.5，权重越高，刷新概率越大，为0则不生成，7是沙子，8是土
            },
            SpawnFunction = ((CreatureType creatureType, Point3 point) => SpawnCreatures(creatureType, "炸麦", point, 1).Count)
        });

        m_creatureTypes.Add(new CreatureType("SCTIOR", SpawnLocationType.Surface, randomSpawn: false, constantSpawn: false)
        {
            SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
            {
                float num91 = m_subsystemTerrain.TerrainContentsGenerator.CalculateOceanShoreDistance(point.X, point.Z); //海岸线距离
                int humidity24 = m_subsystemTerrain.Terrain.GetHumidity(point.X, point.Z); //湿度
                int num92 = Terrain.ExtractContents(m_subsystemTerrain.Terrain.GetCellValueFast(point.X, point.Y - 1, point.Z)); //脚下方块
                                                                                                                                 //return (num91 > 20f && humidity24 > 8 && point.Y < 80 && (num92 == 8 || num92 == 7 || num92 == 3 || num92 == 2)) ? 0.25f : 0f; 原生成条件
                return (num92 == 577 || num92 == 2 || num92 == 7 || num92 == 3) ? 0.09f : 0f; //新生成条件：脚下方块为铁块(id为46)时才会生成，生成权重由0.25改为2.5，权重越高，刷新概率越大，为0则不生成，7是沙子，8是土
            },
            SpawnFunction = ((CreatureType creatureType, Point3 point) => SpawnCreatures(creatureType, "SCTIOR", point, 1).Count)
        });


        m_creatureTypes.Add(new CreatureType("Boy", SpawnLocationType.Surface, randomSpawn: false, constantSpawn: false)
        {
            SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
            {
                float num91 = m_subsystemTerrain.TerrainContentsGenerator.CalculateOceanShoreDistance(point.X, point.Z); //海岸线距离
                int humidity24 = m_subsystemTerrain.Terrain.GetHumidity(point.X, point.Z); //湿度
                int num92 = Terrain.ExtractContents(m_subsystemTerrain.Terrain.GetCellValueFast(point.X, point.Y - 1, point.Z)); //脚下方块
                                                                                                                                 //return (num91 > 20f && humidity24 > 8 && point.Y < 80 && (num92 == 8 || num92 == 7 || num92 == 3 || num92 == 2)) ? 0.25f : 0f; 原生成条件
                return (num92 == 8 || num92 == 2 || num92 == 7 || num92 == 3) ? 0.08f : 0f; //新生成条件：脚下方块为铁块(id为46)时才会生成，生成权重由0.25改为2.5，权重越高，刷新概率越大，为0则不生成，7是沙子，8是土
            },
            SpawnFunction = ((CreatureType creatureType, Point3 point) => SpawnCreatures(creatureType, "Boy", point, 1).Count)
        });

      
    }
}

